package com.example.chartroom1.mapper;

import com.example.chartroom1.model.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper {
    @Select("select content from message " +
            "where sessionId=#{sessionId} order by postTime desc limit 1")
    String getLastMessageBySessionId(int sessionId);

    //获取最近的一百条消息
    @Select("select messageId, fromId, username as fromName, sessionId, content from message, user " +
            "where message.fromId = user.userId and message.sessionId = #{sessionId} " +
            "order by postTime desc limit 100")
    List<Message> getMessageBySession(int sessionId);

    @Insert("insert into message values (null, #{fromId}, #{sessionId}, #{content}, now())")
    void add(Message message);
}
